/**
 * Copyright (c) 2010 Axa Holding Belgium, SA. All rights reserved.
 * This software is the confidential and proprietary information of the AXA Group.
 */
package chabernac.proxyserver;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class AuthenticationFrame extends JDialog{
  private static final long serialVersionUID = -1197510448781933425L;
  private JTextField myUserName = null;
  private JPasswordField myPassword = null;

  
  public AuthenticationFrame(){
    super((JFrame)null, true);
    init();
    buildGUI();
    reset();
    addKeyListeners();
    positionAtCenter();
  }
  
  private void init(){
    myUserName = new JTextField();
    myUserName.setColumns( 20 );
    myPassword = new JPasswordField();
  }
  
  private void addKeyListeners(){
    EnterKeyListener theListener = new EnterKeyListener();
    myPassword.addKeyListener( theListener );
    myUserName.addKeyListener( theListener );
  }
  
  private void positionAtCenter(){
    Toolkit theToolkit = Toolkit.getDefaultToolkit();
    Dimension theDimension = theToolkit.getScreenSize();
    setLocation( (int)(theDimension.getWidth() / 2 - getWidth() / 2), (int)(theDimension.getHeight() / 2 - getHeight() / 2 ));
    
  }
  
  private void buildGUI(){
    setTitle( "Proxy authentication" );
    
    setAlwaysOnTop( true );
    getContentPane().setLayout(  new GridBagLayout() );
    
    GridBagConstraints theCons = new GridBagConstraints();
    theCons.gridx = 0;
    theCons.gridy = 0;
    theCons.gridwidth = 1;
    theCons.gridheight = 1;
    theCons.weightx = 0;
    theCons.weighty = 0;
    theCons.fill = GridBagConstraints.NONE;
    theCons.anchor = GridBagConstraints.WEST;
    theCons.insets = new Insets(10,5,5,5);
    
    getContentPane().add( new JLabel("User name: "), theCons );
    
    theCons.gridy++;
    
    theCons.insets = new Insets(0,5,5,5);
    getContentPane().add( new JLabel("Password: "), theCons );
    
    theCons.gridx++;
    theCons.gridy = 0;
    theCons.fill = GridBagConstraints.HORIZONTAL;
    theCons.weightx = 1;
    theCons.insets = new Insets(10,5,5,5);
    
    getContentPane().add( myUserName, theCons );
    
    theCons.gridy++;
    theCons.insets = new Insets(0,5,5,5);
    
    getContentPane().add( myPassword, theCons );
  
    theCons.gridx = 0;
    theCons.gridy++;
    theCons.gridwidth = 2;
    theCons.insets = new Insets(0,5,5,5);
    theCons.fill = GridBagConstraints.NONE;
    theCons.anchor = GridBagConstraints.EAST;
    
    JButton theOKButton = new JButton(new OKAction());
    getContentPane().add(theOKButton, theCons);
    
    pack();
  }
  
  public String getUserName(){
    return myUserName.getText();
  }
  
  public String getPassword(){
    return new String(myPassword.getPassword());
  }
  
  public class OKAction extends AbstractAction {
    private static final long serialVersionUID = -3642815292961234906L;

    public OKAction(){
      putValue( Action.NAME, "Ok" );
      putValue( Action.MNEMONIC_KEY, KeyEvent.VK_O);
    }

    @Override
    public void actionPerformed( ActionEvent anE ) {
      AuthenticationFrame.this.setVisible( false );
    }
  }
  
  public void reset() {
    SwingUtilities.invokeLater( new Runnable(){
      public void run(){
//        myPassword.setText( "" );
        if(myUserName.getText().equals( "" )){
          myUserName.requestFocus();
        } else {
          myPassword.requestFocus();
        }
      }
    });
  }
  
  public class EnterKeyListener implements KeyListener{

    @Override
    public void keyPressed( KeyEvent anEvent ) {
      if(anEvent.getKeyCode() == KeyEvent.VK_ENTER){
        setVisible( false );
      }
    }

    @Override
    public void keyReleased( KeyEvent anE ) {
    }

    @Override
    public void keyTyped( KeyEvent anE ) {
    }

  }
  
  public static void main(String args[]){
    AuthenticationFrame theFrame = new AuthenticationFrame();
    theFrame.setVisible( true );
  }

}
